package nrpc.register;

import java.util.HashMap;
import java.util.Map;

import nrpc.impl.RegisterHandler;
import nrpc.message.meta.ServerEntry;

public class RegisterHandlerImpl implements RegisterHandler {

	private Map<String, ServiceEntry> serviceMap = new HashMap<>();
	
	public RegisterHandlerImpl(String...urls) {
		for(String x: urls)
			serviceMap.put(x, new ServiceEntry());
	}
	
	@Override
	public long register(String url, ServerEntry serverEntry) {
		return serviceMap.get(url).addServerEntry(serverEntry);
	}

	@Override
	public void cancelService(String url, long id) {
		serviceMap.get(url).deleteServerEntry(id);
	}

	@Override
	public void pulse(String url, long id) {
		serviceMap.get(url).pulse(id);
	}

	@Override
	public long subscribe(String url, String host, int port) {
		return serviceMap.get(url).subscribe(host, port);
	}

	@Override
	public void cancelSubscribe(String url, long id) {
		serviceMap.get(url).cancelSubscribe(id);
	}
	
	public void close() {
		serviceMap.forEach((x, se)->se.close());
	}

}
